import asyncio
from typing import List

from llama_index.core.agent.workflow import  FunctionAgent
from llama_index.core.base.llms.types import ChatMessage
from llama_index.core.storage.chat_store.sql import SQLAlchemyChatStore
from llama_index.core.tools import QueryEngineTool
from llama_index.core.vector_stores import SimpleVectorStore
from llama_index.core.schema import  TextNode
from llama_index.core import Settings, SimpleKeywordTableIndex, SummaryIndex, get_response_synthesizer
from llama_index.embeddings.zhipuai import ZhipuAIEmbedding
from llama_index.core.graph_stores import SimplePropertyGraphStore
from llama_index.core.schema import Document
from pydantic import BaseModel

embed_model = ZhipuAIEmbedding(
    model="embedding-2",
    api_key="f387f5e4837d4e4bba6d267682a957c9.PmPiTw8qVlsI2Oi5"
    # With the `embedding-3` class
    # of models, you can specify the size
    # of the embeddings you want returned.
    # dimensions=1024
)
Settings.embed_model=embed_model

from llama_index.llms.deepseek import DeepSeek

llm = DeepSeek(model="deepseek-chat", api_key="sk-605e60a1301040759a821b6b677556fb")
Settings.llm = llm
from llama_index.core.extractors.metadata_extractors import (
    KeywordExtractor,
    PydanticProgramExtractor,
    QuestionsAnsweredExtractor,
    SummaryExtractor,
    TitleExtractor,
)
from llama_index.core.extractors.document_context import DocumentContextExtractor

from llama_index.core import VectorStoreIndex
from llama_index.core.objects import ObjectIndex, SimpleObjectNodeMapping,SimpleToolNodeMapping
'''
# 定义任意Python对象
obj1 = {"input": "Hey, how's it going"}  # 字典对象
obj2 = ["a", "b", "c", "d"]  # 列表对象
obj3 = "llamaindex is an awesome library333!"  # 字符串对象
obj4 = "llamaindex is an awesome library!"  # 字符串对象
arbitrary_objects = [obj1, obj2, obj3,obj4]

# 构建对象-节点映射
obj_node_mapping = SimpleObjectNodeMapping.from_objects(arbitrary_objects)

# 构建对象索引（底层使用向量索引）
object_index = ObjectIndex(
    index=VectorStoreIndex(nodes=obj_node_mapping.to_nodes(arbitrary_objects)),
    object_node_mapping=obj_node_mapping,
)

# 获取检索器（返回最相似的1个对象）
object_retriever = object_index.as_retriever(similarity_top_k=3)

# 执行检索（查询"llamaindex"相关对象）
result = object_retriever.retrieve("llamaindex")
print("检索结果：", result)



text="""2022年6月14日，江苏南京大报恩塔与“超级月亮”相映成景。新华社发（苏阳 摄）　
　今年农历八月的这次满月，也算是一次“超级月亮”。月球的公转轨道是椭圆形的，地月平均距离约是38万千米，离得最远的时候能达到40万千米，近的时候只有35万多千米。如果月球位于近地点前后又恰逢满月，就是所谓的“超级月亮”。
这次的中秋节和之后的农历八月十六晚上，月球距离我们大约是36万千米。虽然并非全年最大满月，但也算是“排进前三”的“超级月亮”了。"""
node=TextNode(text=text)
keyword_index = SimpleKeywordTableIndex(
    [node],
    show_progress=True,
)
query_engine=keyword_index.as_query_engine()

query1_tool=QueryEngineTool.from_defaults(query_engine,description="关于超级月亮的信息")

# 构建对象-节点映射
obj_node_mapping = SimpleToolNodeMapping.from_objects([query1_tool])

# 构建对象索引（底层使用向量索引）
object_index = ObjectIndex(
    index=VectorStoreIndex(nodes=obj_node_mapping.to_nodes([query1_tool])),
    object_node_mapping=obj_node_mapping,
)

# 获取检索器（返回最相似的1个对象）
object_retriever = object_index.as_retriever(similarity_top_k=1)

# 执行检索（查询"llamaindex"相关对象）
result = object_retriever.retrieve("超级月亮")[0]

ps=result("超级月亮")

print("检索结果：", ps)
'''